<?php
/**
 * 商户
 * User: chen guang
 * Date: 2017/9/29 9:00
 *
 */

namespace app\admin\Controller;


use think\Db;

class Works extends Common{
	
	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();

	}

    /**
     * 风格列表
     */
    public function style(){
        $list = db("works_style")->order("id desc")->paginate(14);
        $this->assign('list',$list);
        return $this->fetch();
    }
	public function styledel(){
		$id = input('id/d');
		$type = input('type/d');
		if($type==1){
			$del = db('works_style')->where("id='$id'")->delete();
			$this->redirect("Works/style");
		}
		if($type==2){
			$del = db('works_space')->where("id='$id'")->delete();
			$this->redirect("Works/space");
		}
		
	}
	public function stylename(){
		$type = input('type/d');
		$id = input('id/d');
		$data['name'] = input('name');
		if($type==1){
			$update = db('works_style')->where("id='$id'")->update($data);
		}
		if($type==2){
			$update = db('works_space')->where("id='$id'")->update($data);
		}
		echo json_encode($update);
	}
	public function add(){
		$type = input('type/d');
		if($type==1){
			$data['name'] = '';
			db('works_style')->insert($data);
			$this->redirect("Works/style");
		}
		if($type==2){
			$data['name'] = '';
			db('works_space')->insert($data);
			$this->redirect("Works/space");
		}
	}
	/**
     * 空间列表
     */
    public function space(){
		$list = db("works_space")->order("id desc")->paginate(14);
        $this->assign('list',$list);
        return $this->fetch();
    }
    
    
	/**
     * 作品列表
     */
    public function lists(){
		$status=input("status");
		if($status!=''){
			$where['d.status']=$status;
		}

		$style=input("style");
		if($style!=''){
			$where['d.style']=$style;
		}

		$space=input("space");
		if($space!=''){
			$where['d.space']=$space;
		}
		
		if(input("title")){
			$where['d.title']=array("like",'%'.input('title').'%');
		}
		$where['d.del']=0;
        $list = db("design_works")
		->alias('d')
		->join('member m','d.uid=m.id')
        ->where($where)
		->field('m.nickname,d.*')
        ->order("d.id desc")
        ->paginate(6);
		
		$works_style = db("works_style")->order("id desc")->select();
		$works_space = db("works_space")->order("id desc")->select();
		
        $this->assign('list',$list);
		$this->assign('status',input("status"));
		$this->assign('style_type',$style);
		$this->assign('space_type',$space);
		$this->assign('style',$works_style);
		$this->assign('space',$works_space);
		$this->assign('title',input("title"));
        return $this->fetch();
    }
    
	/**
     * 作品评论
     */
    public function comment(){
    	$where=array();
		$status=input("status");
		if($status!=''){
			$where['c.status']=$status;
		}
        $list = db("comment_works")
		->alias('c')
		->join('design_works d','c.did=d.id')
		->join('member m','c.uid=m.id')
        ->where($where)
		->field('c.id,d.title,d.images,m.nickname,c.content,c.cname,c.status,c.pid')
        ->order("c.id desc")
        ->paginate(6);
		
        $this->assign('list',$list);
		$this->assign('status',$status);
        return $this->fetch();
    }

	/*
      * 通过认证
      */
	public function ok(){
		$id = input('id/d');
		$type = input('type/d');
		$data['status'] = 1;
		if($type==1){
			$res=db("design_works")->where("id='$id'")->update($data);
			$this->redirect("Works/lists");
		}
		if($type==2){
			$res=db("comment_works")->where("id='$id'")->update($data);
			$this->redirect("Works/comment");
		}
	}
	/*
      * 拒绝认证
      */
	public function no(){
		$id = input('id/d');
		$type = input('type/d');
		$data['status'] = 2;
		if($type==1){
			$res=db("design_works")->where("id='$id'")->update($data);
			$this->redirect("Works/lists");
		}
		if($type==2){
			$res=db("comment_works")->where("id='$id'")->update($data);
			$this->redirect("Works/comment");
		}
	}

	/*
  * setblack
  */
	public function setblack(){
		$id = input('id/d');
		if(!in_array(input('status'),array(0,1))){
			$this->error("状态有误");
		}
		$data['status'] = input('status');
		$res=db("member")->where("id='$id'")->update($data);
		if($res) $this->success("操作成功",url("Center/member"));
		$this->error("操作失败");
	}
	/*
  * 删除会员
  */
	public function del(){
		$id = input('id/d');
		$data['delete'] =0;
		$res=db("member")->where("id='$id'")->update($data);
		if($res) $this->success("操作成功",url("Center/member"));
		$this->error("操作失败");
	}

	/*
      * 测试
      */
	public function test(){
		return $this->fetch();
	}
}